package nl.hanze.designpatterns.DAO.impl.file.ser;

import nl.hanze.designpatterns.DAO.QuestionDAO;
import nl.hanze.designpatterns.DAO.impl.file.BaseFDAOImpl;
import nl.hanze.designpatterns.Domain.Question;

import java.io.*;

/**
 * Created by Aron on 6-10-2014.
 */
public class QuestionFDAOImpl extends BaseFDAOImpl implements QuestionDAO {
    private static final String path = new File("src/main/java/nl/hanze/designpatterns/files").getAbsolutePath().concat("/QuestionFile.file");

    @Override
    public Question getRootQuestion() {
        return Deserialize();
    }

    public static void Serialize(Question question) {
        try
        {
            FileOutputStream fileOut = new FileOutputStream(path);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(question);
            out.close();
            fileOut.close();
        }catch(IOException i)
        {
            i.printStackTrace();
        }
    }

    public static Question Deserialize() {
        Question question = null;
        try
        {
            FileInputStream fileIn = new FileInputStream(path);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            question = (Question) in.readObject();
            in.close();
            fileIn.close();
            return question;
        }catch(IOException i)
        {
            i.printStackTrace();
            return null;
        }catch(ClassNotFoundException c)
        {
            c.printStackTrace();
            return null;
        }
    }
}
